import { ref } from 'vue';
import { SystemNotifyListResult } from '@/apis/workbench/model/workbenchModel';
import { systemNotifyList, systemNotifyDetail } from '@/apis/workbench/index';
import { moduleTypeEnum } from '@/constants/enum';
import { getReleaseInfoDetail } from '@/apis/system/information';
export const useSystem = () => {
  const totalPages = ref(0);
  const total = ref(0);
  const handleCurrentChange = () => {
    loadList();
  };
  const currentPage = ref(1);
  const loading = ref(false);

  // 详情
  const dialogVisible = ref(false);

  const noticeList = ref<SystemNotifyListResult[]>([]);
  // 加载制度
  const loadList = async () => {
    const params = {
      current: currentPage.value,
      size: 6,
      module: moduleTypeEnum.INSTITUTION
    };
    try {
      const data = await systemNotifyList(params);
      noticeList.value = data.records ?? [];
      totalPages.value = data.pages;
      total.value = data.total;
    } finally {
      loading.value = false;
    }
  };

  loadList();
  const dialogMain = ref<any>({});
  const toDialog = (item: SystemNotifyListResult) => {
    if (item.sendType == 'LINK_URL') {
      window.open(item.content);
    } else {
      getReleaseInfoDetail(item.id).then(res => {
        if (item.sendType == 'TEXT_FILE') {
          dialogMain.value = res;
          dialogVisible.value = true;
        } else {
          window.location.href = res.files[0].fileUrl;
        }
      });
    }
  };
  return {
    loading,
    totalPages,
    currentPage,
    dialogVisible,
    handleCurrentChange,
    toDialog,
    dialogMain,
    noticeList,
    total
  };
};
